package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.bank.card.BatchDeleteBankCardDto;
import com.opennews.openplatform.dto.bank.card.CreateBankCardDto;
import com.opennews.openplatform.dto.bank.card.UpdateBankCardDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.BankCardService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/bank-card")
public class BankCardController {
    private final BankCardService bankCardService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-all-with-basic-info-by-account-group")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryAllWithBasicInfoByAccountGroup() {
        List<Map<String, Object>> result = bankCardService.queryAllWithBasicInfoByAccountGroup(requestIdentityExtractor.getClaims().accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-basic-info-by-account-group")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryBasicInfoByAccountGroup(@RequestParam int pageSize, @RequestParam int pageIndex) {
        List<Map<String, Object>> result = bankCardService.queryBasicInfoByAccountGroup(requestIdentityExtractor.getClaims().accountGroupId, pageSize, pageIndex);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-bank-card-info")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryBankCardInfo(@RequestParam String id) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        Map<String, Object> result = bankCardService.queryBankCardInfo(accountGroupId, id);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Object>> add(@Valid @RequestBody CreateBankCardDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        bankCardService.add(accountGroupId, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Object>> update(@Valid @RequestBody UpdateBankCardDto data) {
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        bankCardService.update(accountGroupId, data);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Boolean>> delete(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        boolean result = bankCardService.delete(accountGroupId, id);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/batch-delete")
    public ResponseEntity<ApiResponse<Object>> batchDelete(@Valid @RequestBody BatchDeleteBankCardDto data) {
        List<String> ids = data.getIds();
        String accountGroupId = requestIdentityExtractor.getClaims().accountGroupId;
        bankCardService.delete(accountGroupId, ids);

        return httpResponseHelper.getSuccessResponse();
    }
}
